package org.learn.tankgame4;

//射击子弹
public class Shot implements Runnable {
    int x;//
    int y;//
    int direct = 0;//
    int speed = 2;//
    boolean isLive = true;
    //构造器
    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;


    }

    @Override
    public void run() {//射击
        while (isLive) {

            //线程休眠
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //根据方向改变x，y方向
            switch (direct) {
                case 0://上
                    y -= speed;
                    break;
                case 1: //右
                    x += speed;
                    break;
                case 2: //下
                    y += speed;
                    break;
                case 3: //左
                    x -= speed;
                    break;


                }
            //当子弹碰到画板边界时就应该销毁
            //当子弹碰到敌人的坦克时，也应该被销毁；
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750 && isLive)) {
                isLive = false;
                break;
            }
        }
    }
}